package com.taskcollection.mybitsspring;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.FactoryBean;

import java.io.IOException;
import java.io.Reader;

/**
 * Created by Administrator on 2016/8/14 0014.
 */
public class getSqlSessionFactory implements FactoryBean<org.apache.ibatis.session.SqlSessionFactory> {
    private org.apache.ibatis.session.SqlSessionFactory factory;
    private void createSqlSessionFactory(){
        try {
            Reader inputStream = Resources.getResourceAsReader("mybatis.xml");
            factory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Override
    public org.apache.ibatis.session.SqlSessionFactory getObject() throws Exception {
        if(factory == null){
            createSqlSessionFactory();
        }
        return factory;
    }

    @Override
    public Class<?> getObjectType() {
//        if(factory == null){
//            createSqlSessionFactory();
//        }
        return factory.getClass();
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
